@extends('mobile_end.layouts.default')
{{-- content_header --}}

@section('container')

{{ Form::open(array('action' => array('MobileOrderController@doCreate'))) }}
<div class="ui-navigator">
    <div class="carttop"><i class="icon-shopping-cart icon-2x"></i> 我的购物车</div>
</div>
<ul class="cartlist">
    @foreach($entries as $entry)
    @include('mobile_end.layouts._basket_food_list_li', array('entry'=>$entry->food, 'count' => $entry->count))
    @endforeach
</ul>

<div class="cartdiv tdright">
    <input type="hidden" name="total_price" value="0"/>
    <a href="#" class="button button-rounded button-flat"><i class="icon-shopping-cart"></i> 合计：<span class="total-price">0</span> 元</a>
</div>
<div class="cartdiv tdright">
    <span class="button-wrap">
        <button type="submit" class="button button-block button-pill button-large button-highlight">确认下单</button>
    </span>
</div>

<div class="cartdiv tdright">
    <span class="button-wrap">
        <a onclick="window.history.go(-1)" class="button button-block button-pill button-tiny button-primary"><i class="icon-reply"></i>返回</a>
    </span>
</div>
{{ Form::close() }}

@stop


@section('scripts')

<script>

    caleTotalMoney();

    //数量加减
    $('button.lgplus').on('click', function(){
        var foodId = $(this).siblings("input.food_id").val();
        var countDom = $(this).siblings("input.count");
        var count = parseInt(countDom.val());
        if(count <= 1){
            count = 1;
        }else{
            count -= 1;
        }
        countDom.val(count);
        updateBasketFoodCount(foodId, count);
    });

    $('button.lgminus').on('click', function(){
        var foodId = $(this).siblings("input.food_id").val();
        var countDom = $(this).siblings("input.count");
        var count = parseInt(countDom.val());
        countDom.val(count + 1);
        updateBasketFoodCount(foodId, (count+1));
    });

    $("input.count").on('focusout', function(){
        var foodId = $(this).siblings("input.food_id").val();
        updateBasketFoodCount(foodId, $(this).val());
    });
    //删除购物车中的食物
    var dropFoodUrl = '{{ URL::action("MobileBasketController@dropFoodForJSON") }}';
    $('.btn-remove').on('click', function(){
        var self = $(this);

        var sendData = {
            _token:'{{{ csrf_token() }}}',
            id: $(this).attr('data-id')
        };

        $.ajax({
            url: dropFoodUrl,
            type: 'POST',
            data: sendData,
            dataType: 'json',
            success: function(respData){

                if(respData.result){

                    setTimeout(function(){
                        self.parents('li').remove();
                        //重新计算总价
                        caleTotalMoney();
                    }, 600);

                    self.parents('li').animate({
                        opacity: 0,
                        background: '#FF0000'
                    }, 500, 'ease-out');
                }

            }});
    });

    // 更新购物车中食物的数量
    function updateBasketFoodCount(foodId, count){
        var sendData = {
            _token:'{{{ csrf_token() }}}',
            food_id: foodId,
            count: count
        };

        $.ajax({
            url: '{{ URL::action("MobileBasketController@updateFoodCount") }}',
            type: 'POST',
            data: sendData
        });

        caleTotalMoney();
    }

    //计算总计
    function caleTotalMoney(){
        var totalPriceDom = $('span.total-price');
        var totalPriceInputDom = $('input[name="total_price"]');
        var totalPrice = 0;
        $("input.count").each(function(){
            var price = parseFloat($(this).siblings("input.price").val());
            var count = parseFloat($(this).val());
            totalPrice += price * count;
        });
        totalPriceInputDom.val(floatToMoney(totalPrice, 2));
        totalPriceDom.html(floatToMoney(totalPrice, 2));
    }

</script>

@stop